<#-- 包含其他文件的内容 -->
<#include "../common/header.ftl"/>
<script>
    $(function () {
        $(".inputBtn").click(function () {
            // 清空模态框
            $("#editForm")[0].reset();
            // 清除图片
            $("#coverUrl").attr("src", "");
            // 获取json数据
            var json = $(this).data("json");
            // console.log(json);
            // 将数据设置到模态框中
            if (json) {
                // 封面回显
                $("#coverUrl").attr("src", json.coverUrl);
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='title']").val(json.title);
                $("#editForm input[name='coverUrl']").val(json.coverUrl);
                $("#editForm select[name='catalog.id']").val(json.catalogId);
                $("#editForm input[name='sequence']").val(json.sequence);
                $("#editForm select[name='state']").val(json.state);
                $(".changeBtn").html(" 修改封面");
                // 大战略下拉框回显
                $("#strategySelect").val(json.strategyId);
                // 攻略分类下拉框回显
                $.get("/strategyCatalog/listByStrategyId.do", {"strategyId": json.strategyId}, function (data) {
                    var temp = '';
                    // 将数据设置到攻略分类
                    $.each(data, function (index, ele) {
                        //console.log(index, ele);
                        temp += "<option value=\"" + ele.id + "\">" + ele.name + "</option>";
                    });
                    $("#catalogSelect").html(temp);
                    // 回显操作,一定要在遍历拼接之后
                    $("#catalogSelect").val(json.catalogId);
                });
                // 查询文章的内容
                $.get("/strategyDetail/getContentById.do", {"id": json.id}, function (data) {
                    // 设置到富文本中
                    ck.setData(data.content);
                });
            } else {
                // 设置按钮文字
                $(".changeBtn").html(" 添加封面");
                // 清空富文本内容
                ck.setData("");
            }
            // 显示模态框
            $("#inputModal").modal("show");


        });
        // 提交表单
        $(".submitBtn").click(function () {
            //获取富文本编辑器的内容,设置到表单组件中
            var data = ck.getData();
            $("#editor").html(data);
            $("#editForm").ajaxSubmit(function (data) {
                doSuccess(data);
            });
        });
        // 更换封面
        updateImg(".changeBtn");
    });


    // 二级联动
    $(function () {
        $("#strategySelect").change(function () {
            $.get("/strategyCatalog/listByStrategyId.do", {"strategyId": $(this).val()}, function (data) {
                var temp = '';
                // 将数据设置到攻略分类
                $.each(data, function (index, ele) {
                    //console.log(index, ele);
                    temp += "<option value=\"" + ele.id + "\">" + ele.name + "</option>";
                });
                $("#catalogSelect").html(temp);
            });

        });
    });
</script>
</head>
<body>

<!--图片上传表单-->
<#include "../common/img.ftl"/>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <#-- 往数据模型中填充一个属性 -->
            <#assign menu="strategyDetail"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">攻略文章管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/strategyDetail/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword"
                           value="${(qo.keyword)!}" placeholder="标题/攻略类别">
                </div>
                <button id="btn_query" class="btn btn-default">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a role="button" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </a>
            </form>

            <table class="table table-striped table-hover">
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>封面</th>
                    <th>发布时间</th>
                    <th>排序</th>
                    <th>攻略类别</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>

                <#-- 循环迭代指令 -->
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.title}</td>
                        <td><img src="${entity.coverUrl}" width="50px"/></td>
                        <td>${(entity.releaseTime?string("yyyy-MM-dd HH:mm:ss"))!"暂未发布"}</td>
                        <td>${entity.sequence}</td>
                        <td>${entity.catalog.name}</td>
                        <td>${entity.stateName}</td>
                        <td>
                            <a class="btn btn-info btn-xs inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>
            <#include "../common/page.ftl"/>
        </div>
    </div>
</div>

<!-- 模态框必须作为body的子标签 -->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">新增/编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm">

                    <input type="text" name="id" hidden>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="title">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">封面：</label>
                        <div class="col-sm-6">
                            <img id="coverUrl" width="100%" class="changeBtn"/>
                            <a style="margin-left: 75px" class="btn btn-info glyphicon glyphicon-picture changeBtn"> 更换封面</a>
                            <input type="hidden" class="form-control" name="coverUrl">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">所属攻略：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="strategySelect">
                                <option value="-1">--- 请选择 ---</option>
                                <#list strategys as s>
                                    <option value="${s.id}">${s.title}</option>
                                </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">攻略类别：</label>
                        <div class="col-sm-6">
                            <select class="form-control" id="catalogSelect" name="catalog.id">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">序号：</label>
                        <div class="col-sm-6">
                            <input type="number" class="form-control" name="sequence">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">状态：</label>
                        <div class="col-sm-6">
                            <select class="form-control" name="state">
                                <option value="0">草稿</option>
                                <option value="1">发布</option>
                                <option value="-1">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <textarea name="content.content" id="editor" rows="10" cols="80">
                        </textarea>
                        <script>
                            var ck = CKEDITOR.replace('editor');
                        </script>
                    </div>
                </form>


            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">
                    <span class="glyphicon glyphicon-off"></span> 关闭
                </button>
                <button type="button" class="btn btn-primary submitBtn">
                    <span class="glyphicon glyphicon-saved"></span> 保存
                </button>
            </div>
        </div>
    </div>
</div>


</body>
</html>